namespace ConcurrentBoo

import System.Threading
import Boo.Lang.Useful
import Boo.Lang.Useful.Attributes

callable Function(arg as object) as object
callable Expression() as object
callable Action()

def future(executor as ConcurrentBoo.Executors.IExecutor, e as Expression):
	c = Computation()
	executor.Execute() do:
		compute(e, c)
	return c

def future(e as Expression):
	c = Computation()
	queueUserWorkItem() do:
		compute(e, c)
	return c
	
def asynchronously(action as Action):
	c = Computation()
	queueUserWorkItem() do:
		try:
			action()
			c.Value = Computation.Nothing
		except x:
			c.Failure = x
	return c
	
internal def compute(e as Expression):
	return compute(e, Computation())
	
internal def compute(e as Expression, c as Computation):
	try:
		c.Value = e()
	except x:
		c.Failure = x
	return c
	
def queueUserWorkItem(callback as WaitCallback):
	queued = ThreadPool.QueueUserWorkItem(callback)
	assert queued

[NoTrace]
def ctrace(message as string):
	System.Console.Error.WriteLine("${date.Now} - ${currentThreadId()} - ${message}")
	
[NoTrace]
private def currentThreadId():
	return System.Threading.Thread.CurrentThread.ManagedThreadId

//[assembly: TraceMethodCalls(TraceMethod: ConcurrentBoo.ctrace)]
